﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using TomanuExtensions;

namespace AmiEmuLib.AmigaConfiguration.MountedDevicesConfig
{
    /// <summary>
    /// Represent hardfile.
    /// </summary>
    public class Hardfile : MountedDevice
    {       
        /// <summary>
        /// Allow us to boot with ROM 1.3. In this way we can provide to ROM 1.3 
        /// filesystem library which will be used to read this disk.
        /// </summary>
        public string FileSystem = "";

        /// <summary>
        /// Path to HD image.
        /// </summary>
        public string FilePath = "";

        /// <summary>
        /// Geometrical disk property.
        /// </summary>
        public int Surfaces;

        /// <summary>
        /// Geometrical disk property.
        /// </summary>
        public int Sectors;

        /// <summary>
        /// ???
        /// </summary>
        public int Reserved;

        /// <summary>
        /// Geometrical disk property.
        /// </summary>
        public int BlockSize;

        /// <summary>
        /// ???
        /// </summary>
        public MountedDeviceController Controller;

        /// <summary>
        /// Constructor.
        /// </summary>
        public Hardfile()
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_reader"></param>
        public Hardfile(XmlReader a_reader)
            : base(a_reader)
        {
        }

        /// <summary>
        /// Information about disk parameters are written on disk. Probably true only 
        /// for real amiga hardfiles.
        /// </summary>
        public bool RDB
        {
            get
            {
                return (Surfaces == 0) && (Reserved == 0) && (Sectors == 0);
            }
        }

        protected override void ReadXml(XmlReader a_reader)
        {
            a_reader.ReadStartElement(GetType().Name);

            base.ReadXml(a_reader);

            FileSystem = a_reader.ReadElementContentAsString("FileSystem");
            FilePath = a_reader.ReadElementContentAsString("FilePath");
            Surfaces = a_reader.ReadElementContentAsInt("Surfaces");
            Sectors = a_reader.ReadElementContentAsInt("Sectors");
            Reserved = a_reader.ReadElementContentAsInt("Reserved");
            BlockSize = a_reader.ReadElementContentAsInt("BlockSize");
            Controller = a_reader.ReadElementContentAsEnum<MountedDeviceController>("Controller");

            a_reader.ReadEndElement();
        }

        public override void WriteXml(XmlWriter a_writer)
        {
            a_writer.WriteStartElement(GetType().Name);

            base.WriteXml(a_writer);

            a_writer.WriteElement("FileSystem", FileSystem);
            a_writer.WriteElement("FilePath", FilePath);
            a_writer.WriteElement("Surfaces", Surfaces);
            a_writer.WriteElement("Sectors", Sectors);
            a_writer.WriteElement("Reserved", Reserved);
            a_writer.WriteElement("BlockSize", BlockSize);
            a_writer.WriteElement("Controller", Controller);

            a_writer.WriteEndElement();
        }
    }
}
